<template> 
    <el-card class="form-container" shadow="never">
        <el-form :model="shopStore" :rules="rules" ref="ShopStoreFrom" label-width="150px">


                   <!--
                        <el-form-item
                            label="id"
                            prop="id">
                        <el-input v-model="shopStore.id" style="width: 370px;"/>
                    </el-form-item>
                    -->


                    <el-form-item
                            label="门店名称"
                            prop="name">
                        <el-input v-model="shopStore.name" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="地址细节"
                            prop="address">
                        <el-input v-model="shopStore.address" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="联系人"
                            prop="contactName">
                        <el-input v-model="shopStore.contactName" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="联系电话"
                            prop="contactsTel">
                        <el-input v-model="shopStore.contactsTel" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="经度"
                            prop="longitude">
                        <el-input v-model="shopStore.longitude" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="维度"
                            prop="latitude">
                        <el-input v-model="shopStore.latitude" style="width: 370px;"/>
                    </el-form-item>


                   <!--

                     <el-form-item
                            label="创建者"
                            prop="createBy">
                        <el-input v-model="shopStore.createBy" style="width: 370px;"/>
                    </el-form-item>
                    -->


                  
                    <el-form-item
                            label="创建时间"
                            prop="createTime">
                        <el-date-picker
                            v-model="shopStore.createTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期时间">
                            </el-date-picker>
                    </el-form-item>
                   


                  <!--
                    <el-form-item
                            label="所属商户ID"
                            prop="supplierId">
                        <el-input v-model="shopStore.supplierId" style="width: 370px;"/>
                    </el-form-item>
                     <el-form-item
                            label="所属商户名称"
                            prop="supplierName">
                        <el-input v-model="shopStore.supplierName" style="width: 370px;"/>
                    </el-form-item>
                  -->


                   


            <el-form-item>
                <el-button type="primary" @click="onSubmit('ShopStoreFrom')">提交</el-button>
                <el-button v-if="!isEdit" @click="resetForm('ShopStoreFrom')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
    import {createShopStore, getShopStore, updateShopStore} from '@/api/mer/shopStore'
    import SingleUpload from '@/components/Upload/singleUpload'

    const defaultShopStore = {
        name: ''
    };
    export default {
        name: 'ShopStoreDetail',
        components: {SingleUpload},
        props: {
            isEdit: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
            shopStore:
            Object.assign({},
        defaultShopStore),
            rules: {
                name: [
                    {required: true, message: '请输入品牌名称', trigger: 'blur'},
                    {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
                ],
                    logo
            :
                [
                    {required: true, message: '请输入品牌logo', trigger: 'blur'}
                ],
                    sort
            :
                [
                    {type: 'number', message: '排序必须为数字'}
                ],
            }
        }
        },
        created() {
            if (this.isEdit) {
                getShopStore(this.$route.query.id).then(response => {
                    this.shopStore = response.data;
            })
                ;
            } else {
                this.shopStore = Object.assign({},
            defaultShopStore)
                ;
                //this.getShopLocation()
            }
        },
        methods: {
            getShopLocation(){
                    if (navigator.geolocation){ 
                        navigator.geolocation.getCurrentPosition(function(position){
                            shopStore.longitude = position.coords.longitude
                            shopStore.latitude = position.coords.latitude
                        },function(a){
                            alert(a.message); 
                        }); 
                    }else{ 
                        alert("浏览器不支持地理定位。"); 
                    } 
                    //shopStore
                
            },
        
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if(valid) {
                        this.$confirm('是否提交数据', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            if(this.isEdit
                    )
                        {
                            updateShopStore(this.$route.query.id, this.shopStore).then(response => {
                                if(response.code == 200
                        )
                            {
                                this.$refs[formName].resetFields();
                                this.$message({
                                    message: '修改成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.$router.back();
                            }
                        else
                            {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    duration: 1000
                                });
                            }

                        })
                            ;
                        }
                    else
                        {
                            createShopStore(this.shopStore).then(response => {
                                if(response.code == 200
                        )
                            {
                                this.$refs[formName].resetFields();
                                this.shopStore = Object.assign({},
                            defaultShopStore)
                                ;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.$router.back();
                            }
                        else
                            {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    duration: 1000
                                });
                            }

                        })
                            ;
                        }
                    })
                        ;

                    } else {
                        this.$message({
                            message: '验证失败',
                            type: 'error',
                            duration: 1000
                        });
                return false;
            }
            })
                ;
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.shopStore = Object.assign({},
            defaultShopStore)
                ;
            }
        }
    }
</script>
<style>
</style>


